#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

# Copyright (c) 2019-2020 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


import os
from ... import Category, ItemBase, ScanItemError


CATEGORY = Category(
    name="email-service",
    description="Email service"
)


class Item(ItemBase):

    def __init__(self, name):
        self._hostname = name

    @property
    def name(self):
        return self._hostname

    @property
    def category(self):
        return CATEGORY

    def get_hostname(self):
        return self._hostname


def scan_items(sys_dir_list, etc_dir_list, user_dir_list):
    ret = {}

    for d in sys_dir_list + etc_dir_list + user_dir_list:
        if os.path.isdir(d):
            for fn in os.listdir(d):
                fullfn = os.path.join(d, fn)
                if os.path.isdir(fullfn):
                    raise ScanItemError("%s: Directory is not allowed." % (fullfn))
                with open(fullfn) as f:
                    i = 0
                    for line in f:
                        i += 1
                        line = line.lstrip()                              # remove leading spaces
                        line = line.split("#")[0].rstrip()                # remove comment
                        if line == "":
                            pass                                          # ignore empty line
                        elif line.startswith("-"):
                            if d in sys_dir_list:
                                raise ScanItemError("%s (line %d): -* item is not allowed in system item file." % (fullfn, i))
                            if d in user_dir_list:
                                raise ScanItemError("%s (line %d): -* item is not allowed in user item file." % (fullfn, i))
                            ret.pop(line, None)                           # remove a previously defined item
                        else:
                            ret[line] = Item(line)                        # add item to result

    return ret.values()
